package service

import (
	"git.huoys.com/block_chain/mall/pkg/log"
	"go.uber.org/zap"
)

func (s server) BindPid(uid uint32, pid uint32) (map[uint32]uint32, error) {
	if err := s.repo.BindPid(uid, pid); err != nil {
		return nil, err
	}
	return s.TradeAddPower(uid)
}

func (s server) TradeAddPower(uid uint32) (map[uint32]uint32, error) {
	uids, err := s.GetAllAncestorByUid(uid)
	if err != nil {
		log.GetLogger().Error("Computing power", zap.Any("uid", uid), zap.Error(err))
		return nil, nil
	}
	var data = make(map[uint32]uint32, len(uids))
	for _, v := range uids {
		data[v] = 1
	}
	return data, nil
}

func (s server) GetDescendantByUid(uid uint32) (uint32, error) {
	return s.repo.GetDescendantByUid(uid)
}

func (s server) GetAllDescendantByUid(uid uint32) ([]uint32, error) {
	return s.repo.GetAllDescendantByUid(uid)
}

func (s server) GetAncestorByUid(uid uint32) (uint32, error) {
	return s.repo.GetAncestorByUid(uid)
}

func (s server) GetAllAncestorByUid(uid uint32) ([]uint32, error) {
	return s.repo.GetAllAncestorByUid(uid)
}
